﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AutoPartsSearch.Domain.Common;
using Griffin.MvcContrib.Localization.Types;
using AutoPartsSearch.Domain.Abstract;

namespace AutoPartsSearch.Web.Infrastructure.Cache.Localizations
{
    public class CachedTypeLocalizer : RepositoryStringProvider 
    {
        private readonly int _timeToHoldCache = 5;

        public CachedTypeLocalizer(ILocalizedTypesRepository repository, IConfigurationGetter configurationGetter) : base(repository)
        {
            int value;
            if (int.TryParse(configurationGetter.GetConfigurationValue(Constants.LocalizationCacheInMinutes), out value))
            {
                this._timeToHoldCache = value;
            }
        }

        protected override string Translate(Type type, string name)
        {
            var promptName = type.FullName + "." + name;
            return CacheExecutor.Execute(() => base.Translate(type, name) ?? name, _timeToHoldCache, promptName);
        }
    }
}